package com.infolab.aims.web.servlet;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.infolab.aims.GlobalConstant;
import com.infolab.aims.common.CommonUtil;
import com.infolab.aims.domain.SystemAccessRight;
import com.infolab.aims.domain.SystemUser;
import com.infolab.aims.domain.Vendor;
import com.infolab.aims.domain.asset.AssetModel;
import com.infolab.aims.domain.workorder.WorkOrder;
import com.infolab.aims.service.intf.CommonService;

/**
 * Servlet implementation class InitializerServlet
 */
public class InitializerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	Log logger = LogFactory.getLog(getClass());
	
    @Override
    public void init(ServletConfig config) throws ServletException {

    	super.init(config);
    	logger.info("Initializer servlet");
    	ServletContext servletContext = this.getServletContext();
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_DATE_FORMAT_JAVA, GlobalConstant.DATE_FORMAT_JAVA);
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_DATE_TIME_FORMAT_JAVA, GlobalConstant.DATE_TIME_FORMAT_JAVA);
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_DATE_FORMAT_JSCAL, GlobalConstant.DATE_FORMAT_JSCAL);
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_LOCK, new HashMap<String, String>());
    	
    	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    	CommonService commonService = ctx.getBean(CommonService.class);
    	List list = commonService.getDepartment();
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_DEPARTMENT, list);
    	
    	list = commonService.getSection();
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_SECTION, list);
    	
    	
    	StringWriter writer = new StringWriter();
    	ObjectMapper mapper = new ObjectMapper();
        JsonGenerator jsonGenerator;
		try {
			jsonGenerator = mapper.getJsonFactory().createJsonGenerator(writer);
			mapper.writeValue(jsonGenerator, list);
			getServletContext().setAttribute(GlobalConstant.GLOBAL_SECTION_JSON, writer);
		} catch (IOException e) {
			logger.error(CommonUtil.getStackTrace(e));
		}
    	
    	list = commonService.getLocation();
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_LOCATION, list);
    	
    	list = commonService.getVendor();
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_VENDOR, list);
    	
    	list = commonService.getAssetModel();
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_ASSET_MODEL, list);
    	
    	list = commonService.getRole();
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_ROLE, list);
    	
    	writer = new StringWriter();
    	mapper = new ObjectMapper();
    	try {
			jsonGenerator = mapper.getJsonFactory().createJsonGenerator(writer);
			mapper.writeValue(jsonGenerator, list);
			getServletContext().setAttribute(GlobalConstant.GLOBAL_ROLE_JSON, writer);
		} catch (IOException e) {
			logger.error(CommonUtil.getStackTrace(e));
		}
    	
    	
    	Map<String,String> tmpMap = new TreeMap<String, String>();
    	tmpMap.put("1","Active");
    	tmpMap.put("0","Not Active");
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_ACTIVE_MAP, tmpMap);
    	
    	tmpMap = new TreeMap<String, String>();
    	tmpMap.put("1","Yes");
    	tmpMap.put("0","No");
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_YES_NO_MAP, tmpMap);
    	
    	tmpMap = new TreeMap<String, String>();
    	tmpMap.put(AssetModel.ASSETSTATUS_ACTIVE,"Active");
    	tmpMap.put(AssetModel.ASSETSTATUS_INACTIVE,"Not Active");
    	tmpMap.put(AssetModel.ASSETSTATUS_FAULT,"Fault");
    	tmpMap.put(AssetModel.ASSETSTATUS_REPAIR,"Repair");
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_ASSET_MODEL_STATUS_MAP, tmpMap);
    	
    	tmpMap = new TreeMap<String, String>();
    	tmpMap.put(Vendor.VENDORTYPE_NEW,"New Asset");
    	tmpMap.put(Vendor.VENDORTYPE_REPAIR,"Repair Asset");
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_VENDOR_TYPE_MAP, tmpMap);
    	
    	tmpMap = new TreeMap<String, String>();
    	tmpMap.put(SystemUser.USERTYPE_SYSTEMUSER,"System User");
    	tmpMap.put(SystemUser.USERTYPE_VISITOR,"Visitor");
    	tmpMap.put(SystemUser.USERTYPE_SECURITY_GUARD,"Security Guard");
    	
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_USER_TYPE_MAP, tmpMap);
    	
    	tmpMap = new HashMap<String, String>();
    	
    	tmpMap.put(WorkOrder.WORKORDERTYPE_COMPLAINT,"Complaint");
    	tmpMap.put(WorkOrder.WORKORDERTYPE_WALKIN,"Walk-In");
    	tmpMap.put(WorkOrder.WORKORDERTYPE_OTHER,"Other");
    	
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_WORKORDER_TYPE_MAP, tmpMap);
    	
    	list = commonService.getSystemAccessRight();
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_ACCESS_RIGHT, list);
    	tmpMap = new HashMap<String, String>();
    	if(list!=null)
    	{
    		for(int i=0;i<list.size();i++)
    		{
    			SystemAccessRight systemAccessRight = (SystemAccessRight) list.get(i);
    			tmpMap.put(systemAccessRight.getAccessRightId(), systemAccessRight.getAccessRightName());
    		}
    	}
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_ACCESS_RIGHT_MAP, tmpMap);
    	
    	list = commonService.getSystemCodeValue(GlobalConstant.SYSTEM_CODE_TYPE_ASSET_TYPE);
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_ASSET_TYPE, list);
    	
    	list = commonService.getSystemCodeValue(GlobalConstant.SYSTEM_CODE_TYPE_ASSET_CATEGORY);
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_ASSET_CATEGORY, list);
    	
    	list = commonService.getSystemCodeValue(GlobalConstant.SYSTEM_CODE_TYPE_COMPANY_CODE);
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_COMPANY_CODE, list);
    	
    	list = commonService.getSystemCodeValue(GlobalConstant.SYSTEM_CODE_TYPE_WORK_ORDER_ADHOC);
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_WORK_ORDER_ADHOC, list);
    	
    	list = commonService.getSystemCodeValue(GlobalConstant.SYSTEM_CODE_TYPE_FAULT_CODE);
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_FAULT_CODE, list);
    	
    	list = commonService.getSystemCodeValue(GlobalConstant.SYSTEM_CODE_TYPE_REPAIR_CODE);
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_REPAIR_CODE, list);
    	
    	list = commonService.getSystemCodeValue(GlobalConstant.SYSTEM_CODE_TYPE_FAULT_CODE_CATEGORY);
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_FAULT_CODE_CATEGORY, list);
    	
    	list = commonService.getSystemCodeValue(GlobalConstant.SYSTEM_CODE_TYPE_REPAIR_CODE_CATEGORY);
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_REPAIR_CODE_CATEGORY, list);
    	
    	Boolean activeDirectory = ctx.getBean("activeDirectory",Boolean.class);
    	logger.info("initializer servlet activeDirectory(b1) " +activeDirectory);
    	
    	getServletContext().setAttribute(GlobalConstant.GLOBAL_ACTIVE_DIRECTORY, activeDirectory);
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}


}
